/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.apache.log4j.Logger;

/**
 * A table model dedicated to the edit data structure dialog
 * 
 * @see JDialogEditDataStructure
 * @author stl
 */
public class EditDataStructureTableModel extends AbstractTableModel {
		
    private static Logger logger = Logger.getLogger(EditDataStructureTableModel.class); 

    private static final int NAME_COL =0;
	public static final int TYPE_COL =1;
	
	private static final String[] columnNames = { "Column name", "Column type" };
	
	private List<DataStructureColumnInfo> dscis;	
	private List<DataStructureColumnInfo> deletedDscis =new ArrayList<DataStructureColumnInfo>();
	private DataUIHelper dataUIHelper =new DataUIHelper();
	private boolean dirty =false;	
	
	public boolean isDirty() {
		return dirty;
	}

	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
	
	public void setDataStructureColumnInfos( List<DataStructureColumnInfo> dscis ) {
		this.dscis =dscis;
		
		setDirty( false );
		
        fireTableDataChanged();
	}
	
	public List<DataStructureColumnInfo> getDataStructureColumnInfos() {
		List<DataStructureColumnInfo> all =new ArrayList<DataStructureColumnInfo>();
		all.addAll( dscis );
		all.addAll( deletedDscis );
		
		return all;
	}
										  
	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return dscis !=null ? dscis.size() : 0;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		Object obj =null;
		
		DataStructureColumnInfo dsci =dscis.get( row );
		
		switch( col ) {
			case NAME_COL:
				obj =dsci.getUpdatedName();
				break;
		
			case TYPE_COL:
				obj =dataUIHelper.getUIStringForAttrType( dsci.getUpdatedType() );
				break;
		}
		
		return obj;
	}
	
    public void setValueAt(Object value, int row, int col) {
		
    	DataStructureColumnInfo dsci =dscis.get( row );
    	
		switch( col ) {
			case NAME_COL:
				dsci.setUpdatedName( (String)value );
				break;
				
			case TYPE_COL:
				dsci.setUpdatedType( dataUIHelper.getAttrTypeForUIString( (String)value ) );
				break;
		}
    	                    
        fireTableCellUpdated(row, col);
        
        setDirty( true );
    }	

	/*
	 * Don't need to implement this method unless your table's
	 * editable.
	 */
	public boolean isCellEditable(int row, int col) {
		return true; 
	}
	
    @SuppressWarnings("unchecked")
	public int insertRowAt(int idx ) {
    	int rowInsertedAt =idx;
        
        DataStructureColumnInfo newRow =new DataStructureColumnInfo();
        newRow.setNeww( true );
        
        if ( idx != -1 ) {
        	dscis.add( idx, newRow );
            fireTableRowsInserted( idx, idx ); 
        } else {
        	dscis.add( newRow );
            rowInsertedAt =dscis.size()-1;
            fireTableRowsInserted( dscis.size() -1, dscis.size() -1 );
        }
        
        setDirty( true );        
        
        fireTableDataChanged();
        
        return rowInsertedAt;
    }	
	
    public void removeRows(int[] rowsIdx) {
        for (int ct = rowsIdx.length - 1; ct >= 0; ct--) {
        	DataStructureColumnInfo dsci =dscis.get( rowsIdx[ct] );
        	dsci.setDeleted( true );
        	deletedDscis.add( dsci );
        	dscis.remove( rowsIdx[ct] );
            fireTableRowsDeleted( rowsIdx[ct], rowsIdx[ct] );
        }
        
        setDirty( true );
        
        fireTableDataChanged();
    }

     
}
